#include "stdafx.h"
#include "IndexHelper.h"

IndexHelper::IndexHelper()
{
	SetPath(_T("index.ini"));
}

void IndexHelper::CreateIndex()
{
	//MaxID
	Write(_T("MaxID"), _T("ID"), _T("0"));
}

void IndexHelper::GetMonitors(vector<MonitorParameter> &monitors)
{
	int MaxID;
	MonitorParameter monitor;

	ReadInt(_T("MaxID"), _T("ID"), MaxID);
	for (int i = 1; i <= MaxID; i++)
	{
		if (GetMonitor(i, monitor))
			monitors.push_back(monitor);
	}
}

void IndexHelper::RefreshMonitors(vector<MonitorParameter>& monitors)
{
	monitors.clear();
	GetMonitors(monitors);
}

BOOL IndexHelper::GetMonitor(int ID, MonitorParameter &monitor)
{
	vector<wstring> paras;
	CString IdStr, monitorStr;
	
	IdStr.Format(_T("%d"), ID);
	ReadStr(_T("Monitors"), IdStr.GetString(), monitorStr);
	if (_T("") == monitorStr)
		return FALSE;

	paras = FileCommon::Split(monitorStr.GetString(), '*');
	monitor.ID = Parse::WstringToInt(paras[0]);
	monitor.name = paras[1];
	monitor.directory = paras[2];
	monitor.status = Parse::WstringToInt(paras[3]);
	return TRUE;
}

void IndexHelper::UpdateIndex(CString ID, CString value, Field filed)
{
	CString IdStr, monitorStr;
	vector<wstring> paras;
	ReadStr(_T("Monitors"), ID.GetString(), monitorStr);
	paras = FileCommon::Split(monitorStr.GetString(), '*');

	switch (filed)
	{
		case name:
			paras[1] = value; break;
		case directory:
			paras[2] = value; break;
		case status:
			paras[3] = value; break;
		default: break;
	}

	monitorStr.Format(_T("%s*%s*%s*%s"), paras[0].c_str(),paras[1].c_str(),paras[2].c_str(),paras[3].c_str());
	WriteStr(_T("Monitors"), ID.GetString(), monitorStr);
}
